package com.tanhua.app.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.tanhua.api.db.UserInfoApi;
import com.tanhua.api.mongo.FriendApi;
import com.tanhua.api.mongo.MessagesApi;
import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.vo.ContactVo;
import com.tanhua.domain.vo.PageBeanVo;
import com.tanhua.domain.vo.UserCommentVo;
import com.tanhua.template.HuanXinTemplate;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MessagesService {

    @Reference
    private FriendApi friendApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @Reference
    private UserInfoApi userInfoApi;
    @Reference
    private MessagesApi messagesApi;
    //添加好友
    public ResponseEntity contacts(Long userId) {
        //1 添加好友到mongo  自己的id                    对方的id
        friendApi.contacts(UserHolder.getUser().getId(),userId);
        //2 添加好友到环信
        huanXinTemplate.addContacts(UserHolder.getUser().getId(),userId);
        return ResponseEntity.ok(null);
    }

    // 查看登录用户的好友列表
    public ResponseEntity findContacts(Integer page, Integer pagesize, String keyword) {
        // 1 获取登录用户的所有好友分页
        PageBeanVo pb=friendApi.findContacts(page,pagesize,UserHolder.getUser().getId());
        List<Friend> items = (List<Friend>) pb.getItems();

        List<ContactVo> contactVoList=new ArrayList();
        if(CollectionUtil.isNotEmpty(items)){
            for (Friend friend : items) {
                Long friendId = friend.getFriendId(); //每一个好友的id
                UserInfo userInfo = userInfoApi.findUserInfoVo(friendId);//查询用户信息
                //3 封装app需要用到的vo
                ContactVo contactVo = new ContactVo();
                BeanUtils.copyProperties(userInfo,contactVo);
                contactVo.setUserId(userInfo.getId().toString());
                contactVo.setCity(userInfo.getCity().split("-")[1]);

                contactVoList.add(contactVo);
            }
        }

        pb.setItems(contactVoList);
        //4 返回
        return ResponseEntity.ok(pb);
    }

    //统计点赞 喜欢 评论
    public ResponseEntity findComment(Integer page, Integer pagesize, Long commentTypeId) {

        //1 分页查询quanzi_comment的数据
        PageBeanVo pbvo=messagesApi.findComment(page,pagesize,commentTypeId,UserHolder.getUser().getId());
        //根据userid查询对应的UserInfo
        List<Comment> items = (List<Comment>) pbvo.getItems();

        List<UserCommentVo> userCommentVoList=new ArrayList<>();
        if(CollectionUtil.isNotEmpty(items)){
            for (Comment comment : items) {
                //获取userInfo(头像 昵称)
                UserInfo userInfo = userInfoApi.findUserInfoVo(comment.getUserId());
                //封装
                UserCommentVo userCommentVo = new UserCommentVo();
                BeanUtils.copyProperties(userInfo,userCommentVo);
                //手动封装
                userCommentVo.setCreateDate(DateUtil.format(new Date(comment.getCreated()),"yyyy-MM-dd HH:mm"));
                userCommentVoList.add(userCommentVo);
            }
        }
        pbvo.setItems(userCommentVoList);
        return ResponseEntity.ok(pbvo);

    }
}
